﻿#include "FITKRadiossDataCaseAdaptor.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Writer.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKHDF5FileTool.h"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"

//RadiossData
#include "FITK_Component/FITKRadiossData/FITKRadiossCase.h"
#include "FITK_Component/FITKRadiossData/FITKMaterialElastic.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossMeshModel.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossNodesParts.h"
#include "FITK_Component/FITKRadiossData/FITKPropAbstract.hpp"
#include "FITK_Component/FITKRadiossData/FITKNonLinerCurve.h"
#include "FITK_Component/FITKRadiossData/FITKAbstractRWall.h"
#include "FITK_Component/FITKRadiossData/FITKGravity.h"
#include "FITK_Component/FITKRadiossData/FITKAbstractInitialField.h"
#include "FITK_Interface/FITKInterfaceModel/FITKComponentManager.h"
#include "FITK_Component/FITKRadiossData/FITKAbstractBCS.h"
#include "FITK_Component/FITKRadiossData/FITKAbstractInteraction.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossSurfaceSeg.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossSolution.h"
#include "FITK_Component/FITKRadiossData/FITKAbstractProbe.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossNodeGroup.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"
#include "FITK_Component/FITKRadiossData/FITKMaterialPlasJohns.h"
#include "FITK_Component/FITKRadiossData/FITKMaterialPlasTab.h"
#include "FITK_Component/FITKRadiossData/FITKPropShell.h"
#include "FITK_Component/FITKRadiossData/FITKPropSolid.h"
#include "FITK_Component/FITKRadiossData/FITKPropBeamSPR.h"
#include "FITK_Component/FITKRadiossData/FITKInteractionTied.h"
#include "FITK_Component/FITKRadiossData/FITKInitialField.h"
#include "FITK_Component/FITKRadiossData/FITKRWall.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossSolverSettings.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossResultRequest.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossMeshModel.h"

#include "FITK_Component/FITKRadiossDataIOHDF5/FITKRadiossDataPartAdaptor.h"
#include "FITK_Component/FITKRadiossDataIOHDF5/FITKRadiossDataNodeAdaptor.h"
#include "FITK_Component/FITKRadiossDataIOHDF5/FITKRadiossDataSetAdaptor.h"
#include "FITK_Component/FITKRadiossDataIOHDF5/FITKRadiossDataSubSetAdaptor.h"
#include "FITK_Component/FITKRadiossDataIOHDF5/FITKRadiossDataSurfaceAdaptor.h"
#include "FITK_Component/FITKRadiossDataIOHDF5/FITKRadiossDataNonLinerCurveAdaptor.h"
#include "FITK_Component/FITKRadiossDataIOHDF5/FITKRadiossDataInitialFieldTraAdaptor.h"
#include "FITK_Component/FITKRadiossDataIOHDF5/FITKRadiossDataGravityAdaptor.h"
#include "FITK_Component/FITKRadiossDataIOHDF5/FITKRadiossDataSolverSettingsAdaptor.h"
#include "FITK_Component/FITKRadiossDataIOHDF5/FITKRadiossDataResultRequestAdaptor.h"

namespace Interface
{
    bool FITKRadiossDataCaseAdaptor::adaptR()
    {
        _caseData = Radioss::FITKRadiossCase::SafeDownCastFITKRadiossCase(_dataObj);
        if (_reader == nullptr || _caseData == nullptr || _h5Group == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _reader->getHDF5FileTool();
        if (h5tools == nullptr) return false;
        bool ok = true;
        //读取节点
        ok &= readNode(*_h5Group);
        //读取节点组(节点组要在初始场，重力，刚性墙，边界，相互作用前面)
        ok &= readNodeSet(*_h5Group);
        //写出曲线(曲线要在重力，材料(/MAT/PLAS_TAB)，属性(/PROP/SPR_BEAM)前面)
        ok &= readCurve(*_h5Group);
        //写出材料(材料要在Part 前面)
        ok &= readMaterials(*_h5Group);
        //写出属性(截面)(属性要在Part 前面)
        ok &= readProperty(*_h5Group);
        //读取part（Part要在surf前面）
        ok &= readPart(*_h5Group);
        //读取子集
        ok &= readSubSet(*_h5Group);
        //读取surface（surface要在相互作用前面）
        ok &= readSurface(*_h5Group);
        //读取相互作用（）
        ok &= readInteraction(*_h5Group);
        //读取初始场
        ok &= readInitialField(*_h5Group);
        //读取重力
        ok &= readGravity(*_h5Group);
        //读取刚性墙
        ok &= readRigidWall(*_h5Group);

        ok &= readSolverSetting(*_h5Group);
        ok &= readResultRequest(*_h5Group);
        return ok;
    }

    bool FITKRadiossDataCaseAdaptor::adaptW()
    {
        _caseData = Radioss::FITKRadiossCase::SafeDownCastFITKRadiossCase(_dataObj);
        if (_writer == nullptr || _caseData == nullptr || _h5Group == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _writer->getHDF5FileTool();
        if (h5tools == nullptr) return false;
        
        bool ok = true;
        //写出节点
        ok &= writeNode(*_h5Group);
        //写出节点组(节点组要在初始场，重力，刚性墙，边界，相互作用前面)
        ok &= writeNodeSet(*_h5Group);
        //写出曲线(曲线要在重力，材料(/MAT/PLAS_TAB)，属性(/PROP/SPR_BEAM)前面)
        ok &= writeCurve(*_h5Group);
        //写出材料(材料要在Part 前面)
        ok &= writeMaterials(*_h5Group);
        //写出属性(截面)(属性要在Part 前面)
        ok &= writeProperty(*_h5Group);
        //写出part（Part要在surf前面）
        ok &= writePart(*_h5Group);
        //写出子集
        ok &= writeSubSet(*_h5Group);
        //写出surface（surface要在相互作用前面）
        ok &= writeSurface(*_h5Group);
        ////写出全局坐标系
        //// ok &=  （坐标系要在初始场和边界前面）
        //写出相互作用（）
        ok &= writeInteraction(*_h5Group);
        //写出初始场
        ok &= writeInitialField(*_h5Group);
        //写出重力
        ok &= writeGravity(*_h5Group);
        //写出刚性墙
        ok &= writeRigidWall(*_h5Group);
        ////写出边界
        //ok &= writeBC();
        ////写出探针
        //ok &= writeProbe();
        ok &= writeSolverSetting(*_h5Group);
        ok &= writeResultRequest(*_h5Group);

        return ok;
    }
    bool FITKRadiossDataCaseAdaptor::readPart(H5::Group& g)
    {
        H5::Group group = g.openGroup("Part");
        bool isReadSuccess = true;
        Radioss::FITKRadiossMeshModel* meshModel = _caseData->getMeshModel();
        if (!meshModel)return false;
        Radioss::FITKRadiossPartManager* radiossPartManager = meshModel->getPartsManager();
        if (!radiossPartManager)return false;
        bool isR = true;
        // 获取所有子组
        hsize_t numObjs = group.getNumObjs();
        for (hsize_t i = 0; i < numObjs; i++)
        {
            // 获取子组名称
            std::string objName = group.getObjnameByIdx(i);
            H5::Group partGroup = group.openGroup(objName);
            Radioss::FITKRadiossPart* part = new Radioss::FITKRadiossPart();
            part->setDataObjectName(QString::fromStdString(objName));
            // 创建并配置适配器
            Interface::FITKRadiossDataPartAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataPartAdaptor>("HDF5", part);
            if (adaptor == nullptr) {
                isR = false;
                delete part;
                continue;
            }
            adaptor->setH5Group(&partGroup);
            adaptor->setDataObject(part);
            adaptor->setReader(_reader);
            bool r = adaptor->adaptR();
            isR &= r;
            if (r) radiossPartManager->appendDataObj(part);
            else delete part;
            delete adaptor;
        }
        Radioss::FITKRadiossMeshTopo* meshTopo = meshModel->getMeshTopo();
        if(meshTopo)
            meshTopo->updateAllMeshTopo();
        return isR;
    }

    bool FITKRadiossDataCaseAdaptor::writePart(H5::Group& g)
    {
        H5::Group group =g.createGroup("Part");
        bool isW = true;
        Radioss::FITKRadiossMeshModel* meshModel = _caseData->getMeshModel();
        if (!meshModel)return false;
        Radioss::FITKRadiossPartManager* radiossPartManager = meshModel->getPartsManager();
        if (!radiossPartManager) return false;
        int count = radiossPartManager->getDataCount();
        for (int i = 0; i < count; i++)
        {
            Radioss::FITKRadiossPart* part = radiossPartManager->getDataByIndex(i);
            if (!part) return false;
            QString name = part->getDataObjectName();
            H5::Group partGroup = group.createGroup(name.toStdString());
            Interface::FITKRadiossDataPartAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataPartAdaptor>("HDF5", part);
            if (adaptor == nullptr) return false;
            adaptor->setH5Group(&partGroup);
            adaptor->setDataObject(part);
            adaptor->setWriter(_writer);
            bool w = adaptor->adaptW();
            isW &= w;
            delete adaptor;
        }
        return isW;
    }

    bool FITKRadiossDataCaseAdaptor::readNode(H5::Group& g)
    {
        H5::Group group = g.openGroup("Node");
        bool isR = true;
        Radioss::FITKRadiossMeshModel* meshModel = _caseData->getMeshModel();
        if (!meshModel)return false;
        Radioss::FITKRadiossNodes* radiossNodes = meshModel->getNodes();
        if (!radiossNodes)return false;
        Interface::FITKRadiossDataNodeAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataNodeAdaptor>("HDF5", radiossNodes);
        if (adaptor == nullptr) return false;
        adaptor->setH5Group(&group);
        adaptor->setDataObject(radiossNodes);
        adaptor->setReader(_reader);
        bool r = adaptor->adaptR();
        isR &= r;
        delete adaptor;
        return isR;
    }

    bool FITKRadiossDataCaseAdaptor::writeNode(H5::Group& g)
    {
        H5::Group group = g.createGroup("Node");
        bool isW = true;
        Radioss::FITKRadiossMeshModel* meshModel = _caseData->getMeshModel();
        if (!meshModel)return false;
        Radioss::FITKRadiossNodes* radiossNodes = meshModel->getNodes();
        if (!radiossNodes)return false;
        Interface::FITKRadiossDataNodeAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataNodeAdaptor>("HDF5", radiossNodes);
        if (adaptor == nullptr) return false;
        adaptor->setH5Group(&group);
        adaptor->setDataObject(radiossNodes);
        adaptor->setWriter(_writer);
        bool w = adaptor->adaptW();
        isW &= w;
        delete adaptor;
        return isW;
    }

    bool FITKRadiossDataCaseAdaptor::readNodeSet(H5::Group& g)
    {
        H5::Group group = g.openGroup("Set");
        bool isR = true;
        Radioss::FITKRadiossMeshModel* meshModel = _caseData->getMeshModel();
        if (!meshModel) return false;
        Interface::FITKComponentManager* componentManager = meshModel->getComponentManager();
        if (!componentManager) return false;
        // 获取所有子组
        hsize_t numObjs = group.getNumObjs();
        for (hsize_t i = 0; i < numObjs; i++)
        {
            // 获取子组名称
            std::string objName = group.getObjnameByIdx(i);
            H5::Group setGroup = group.openGroup(objName);
            Radioss::FITKRadiossNodeGroup* set = new Radioss::FITKRadiossNodeGroup();
            set->setDataObjectName(QString::fromStdString(objName));
            // 创建并配置适配器
            Interface::FITKRadiossDataSetAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataSetAdaptor>("HDF5", set);
            if (adaptor == nullptr) {
                isR = false;
                delete set;
                continue;
            }
            adaptor->setH5Group(&setGroup);
            adaptor->setDataObject(set);
            adaptor->setReader(_reader);
            bool r = adaptor->adaptR();
            isR &= r;
            if (r)
            {
                set->setModel(meshModel->getDataObjectID());
                componentManager->appendDataObj(set);
            }
            else
                delete set;
            delete adaptor;
        }
        return isR;
    }

    bool FITKRadiossDataCaseAdaptor::writeNodeSet(H5::Group& g)
    {
        H5::Group group = g.createGroup("Set");
        bool isW = true;
        Radioss::FITKRadiossMeshModel* meshModel = _caseData->getMeshModel();
        if (!meshModel)return false;
        Interface::FITKComponentManager* componentManager = meshModel->getComponentManager();
        if (!componentManager)return false;
        QList<FITKModelSet*> setList = componentManager->getAllSet(false);
        int count = setList.size();
        for (Interface::FITKModelSet* set: setList)
        {
            if (!set) return false;
            Radioss::FITKRadiossNodeGroup* nodeGroup = dynamic_cast<Radioss::FITKRadiossNodeGroup*>(set);
            if (!nodeGroup) return false;
            QString name = nodeGroup->getDataObjectName();
            H5::Group setGroup = group.createGroup(name.toStdString());
            Interface::FITKRadiossDataSetAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataSetAdaptor>("HDF5", nodeGroup);
            if (adaptor == nullptr) return false;
            adaptor->setH5Group(&setGroup);
            adaptor->setDataObject(nodeGroup);
            adaptor->setWriter(_writer);
            bool w = adaptor->adaptW();
            isW &= w;
            delete adaptor;
        }
        return isW;
    }

    bool FITKRadiossDataCaseAdaptor::readSubSet(H5::Group& g)
    {
        H5::Group group = g.openGroup("SubSet");
        bool isR = true;
        Radioss::FITKRadiossMeshModel* meshModel = _caseData->getMeshModel();
        if (!meshModel) return false;
        Radioss::FITKRadiossSubSetManager* subSetManager = meshModel->getSubSetManager();
        if (!subSetManager)return false;
        // 获取所有子组
        hsize_t numObjs = group.getNumObjs();
        for (hsize_t i = 0; i < numObjs; i++)
        {
            // 获取子组名称
            std::string objName = group.getObjnameByIdx(i);
            H5::Group subSetGroup = group.openGroup(objName);
            Radioss::FITKRadiossSubSet* subSet = new Radioss::FITKRadiossSubSet();
            subSet->setDataObjectName(QString::fromStdString(objName));
            // 创建并配置适配器
            Interface::FITKRadiossDataSetAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataSetAdaptor>("HDF5", subSet);
            if (adaptor == nullptr) {
                isR = false;
                delete subSet;
                continue;
            }
            adaptor->setH5Group(&subSetGroup);
            adaptor->setDataObject(subSet);
            adaptor->setReader(_reader);
            bool r = adaptor->adaptR();
            isR &= r;
            if (r)
            {
                subSetManager->appendDataObj(subSet);
            }
            else
                delete subSet;
            delete adaptor;
        }
        return isR;
    }

    bool FITKRadiossDataCaseAdaptor::writeSubSet(H5::Group& g)
    {
        H5::Group group = g.createGroup("SubSet");
        bool isW = true;
        Radioss::FITKRadiossMeshModel* meshModel = _caseData->getMeshModel();
        if (!meshModel)return false;
        Radioss::FITKRadiossSubSetManager* subSetManager = meshModel->getSubSetManager();
        if (!subSetManager)return false;
        int count = subSetManager->getDataCount();
        for (int i=0;i<count;i++)
        {
            Radioss::FITKRadiossSubSet* subSet = subSetManager->getDataByIndex(i);
            if (!subSet) return false;
            QString name = subSet->getDataObjectName();
            H5::Group subSetGroup = group.createGroup(name.toStdString());
            Interface::FITKRadiossDataSubSetAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataSubSetAdaptor>("HDF5", subSet);
            if (adaptor == nullptr) return false;
            adaptor->setH5Group(&subSetGroup);
            adaptor->setDataObject(subSet);
            adaptor->setWriter(_writer);
            bool w = adaptor->adaptW();
            isW &= w;
            delete adaptor;
        }
        return isW;
    }

    bool FITKRadiossDataCaseAdaptor::readSurface(H5::Group& g)
    {
        H5::Group group = g.openGroup("Surface");
        bool isR = true;
        Radioss::FITKRadiossMeshModel* meshModel = _caseData->getMeshModel();
        if (!meshModel) return false;
        Interface::FITKComponentManager* componentManager = meshModel->getComponentManager();
        if (!componentManager) return false;
        // 获取所有子组
        hsize_t numObjs = group.getNumObjs();
        for (hsize_t i = 0; i < numObjs; i++)
        {
            // 获取子组名称
            std::string objName = group.getObjnameByIdx(i);
            H5::Group surfaceGroup = group.openGroup(objName);
            Radioss::FITKRadiossSurfaceSeg* surfaceSeg = new Radioss::FITKRadiossSurfaceSeg();
            surfaceSeg->setDataObjectName(QString::fromStdString(objName));
            // 创建并配置适配器
            Interface::FITKRadiossDataSurfaceAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataSurfaceAdaptor>("HDF5", surfaceSeg);
            if (adaptor == nullptr) {
                isR = false;
                delete surfaceSeg;
                continue;
            }
            adaptor->setH5Group(&surfaceGroup);
            adaptor->setDataObject(surfaceSeg);
            adaptor->setReader(_reader);
            bool r = adaptor->adaptR();
            isR &= r;
            if (r)
            {
                surfaceSeg->setModel(meshModel->getDataObjectID());
                componentManager->appendDataObj(surfaceSeg);
            }
            else
                delete surfaceSeg;
            delete adaptor;
        }
        return isR;
    }

    bool FITKRadiossDataCaseAdaptor::writeSurface(H5::Group& g)
    {
        H5::Group group = g.createGroup("Surface");
        bool isW = true;
        Radioss::FITKRadiossMeshModel* meshModel = _caseData->getMeshModel();
        if (!meshModel)return false;
        Interface::FITKComponentManager* componentManager = meshModel->getComponentManager();
        if (!componentManager)return false;
        QList<FITKMeshSurface*> surfaceList = componentManager->getAllMeshSurface();
        int count = surfaceList.size();
        for (Interface::FITKMeshSurface* surface : surfaceList)
        {
            if (!surface) return false;
            Radioss::FITKRadiossSurfaceSeg* surfaceSeg = dynamic_cast<Radioss::FITKRadiossSurfaceSeg*>(surface);
            if (!surfaceSeg) return false;
            QString name = surfaceSeg->getDataObjectName();
            H5::Group surfaceGroup = group.createGroup(name.toStdString());
            Interface::FITKRadiossDataSurfaceAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataSurfaceAdaptor>("HDF5", surfaceSeg);
            if (adaptor == nullptr) return false;
            adaptor->setH5Group(&surfaceGroup);
            adaptor->setDataObject(surfaceSeg);
            adaptor->setWriter(_writer);
            bool w = adaptor->adaptW();
            isW &= w;
            delete adaptor;
        }
        return isW;
    }

    bool FITKRadiossDataCaseAdaptor::readMaterials(H5::Group& g)
    {
        H5::Group group = g.openGroup("Materials");
        bool isR = true;
        Interface::FITKMaterialManager* materialsMgr = _caseData->getMaterialManager();
        if (materialsMgr == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _reader->getHDF5FileTool();
        if (h5tools == nullptr) return false;
        // 获取所有子组
        hsize_t numObjs = group.getNumObjs();
        for (hsize_t i = 0; i < numObjs; i++)
        {
            // 获取子组名称
            std::string objName = group.getObjnameByIdx(i);
            H5::Group materialGroup = group.openGroup(objName);
            std::string type{};
            h5tools->readGroupStr(materialGroup, "MaterialType", type);
            Interface::FITKAbstractMaterial* material = nullptr;
            if (type == "/MAT/LAW0")
                material = new Radioss::FITKMaterialVoid();
            else if (type == "/MAT/LAW1")
                material = new Radioss::FITKMaterialElastic();
            else if (type == "/MAT/LAW2")
                material = new Radioss::FITKMaterialPlasJohns();
            else if (type == "/MAT/LAW36")
                material = new Radioss::FITKMaterialPlasTab();

            if (!material) return false;
            material->setDataObjectName(QString::fromStdString(objName));
            // 创建并配置适配器
            Interface::FITKRadiossDataHDF5Adaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataHDF5Adaptor>("HDF5", material);
            if (adaptor == nullptr) {
                isR = false;
                delete material;
                continue;
            }
            adaptor->setH5Group(&materialGroup);
            adaptor->setDataObject(material);
            adaptor->setReader(_reader);
            bool r = adaptor->adaptR();
            isR &= r;
            if (r)
            {
                materialsMgr->appendDataObj(material);
            }
            else
                delete material;
            delete adaptor;
        }
        return isR;
    }

    bool FITKRadiossDataCaseAdaptor::writeMaterials(H5::Group& g)
    {
        H5::Group group = g.createGroup("Materials");
        bool isW = true;
        Interface::FITKMaterialManager* materialsMgr = _caseData->getMaterialManager();
        if (materialsMgr == nullptr) return false;
        int count = materialsMgr->getDataCount();
        for (int i = 0; i < count; i++)
        {
            Interface::FITKAbstractMaterial* material = materialsMgr->getDataByIndex(i);
            if (!material) return false;
            QString name = material->getDataObjectName();
            H5::Group materialGroup = group.createGroup(name.toStdString());
            Interface::FITKRadiossDataHDF5Adaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataHDF5Adaptor>("HDF5", material);
            if (adaptor == nullptr) return false;
            adaptor->setH5Group(&materialGroup);
            adaptor->setDataObject(material);
            adaptor->setWriter(_writer);
            bool w = adaptor->adaptW();
            isW &= w;
            delete adaptor;
        }
        return isW;
    }

    bool FITKRadiossDataCaseAdaptor::readProperty(H5::Group& g)
    {
        H5::Group group = g.openGroup("Prop");
        bool isR = true;
        Interface::FITKAbstractSectionManager* propMgr = _caseData->getSectionManager();
        if (propMgr == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _reader->getHDF5FileTool();
        if (h5tools == nullptr) return false;
        // 获取所有子组
        hsize_t numObjs = group.getNumObjs();
        for (hsize_t i = 0; i < numObjs; i++)
        {
            // 获取子组名称
            std::string objName = group.getObjnameByIdx(i);
            H5::Group sectionlGroup = group.openGroup(objName);
            std::string type{};
            h5tools->readGroupStr(sectionlGroup, "PropertyType", type);
            Interface::FITKAbstractSection* section = nullptr;
            if (type == "/PROP/TYPE1")
                section = new Radioss::FITKPropShell();
            else if (type == "/PROP/TYPE14")
                section = new Radioss::FITKPropSolid();
            else if (type == "/PROP/TYPE13")
                section = new Radioss::FITKPropBeamSPR();

            if (!section) return false;
            section->setDataObjectName(QString::fromStdString(objName));
            // 创建并配置适配器
            Interface::FITKRadiossDataHDF5Adaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataHDF5Adaptor>("HDF5", section);
            if (adaptor == nullptr) {
                isR = false;
                delete section;
                continue;
            }
            adaptor->setH5Group(&sectionlGroup);
            adaptor->setDataObject(section);
            adaptor->setReader(_reader);
            bool r = adaptor->adaptR();
            isR &= r;
            if (r)
            {
                propMgr->appendDataObj(section);
            }
            else
                delete section;
            delete adaptor;
        }
        return isR;
    }
    bool FITKRadiossDataCaseAdaptor::writeProperty(H5::Group& g)
    {
        H5::Group group = g.createGroup("Prop");
        bool isW = true;
        Interface::FITKAbstractSectionManager* propMgr = _caseData->getSectionManager();
        if (propMgr == nullptr) return false;
        int count = propMgr->getDataCount();
        for (int i = 0; i < count; i++)
        {
            Interface::FITKAbstractSection* section = propMgr->getDataByIndex(i);
            if (!section) return false;
            QString name = section->getDataObjectName();
            H5::Group sectionGroup = group.createGroup(name.toStdString());
            Interface::FITKRadiossDataHDF5Adaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataHDF5Adaptor>("HDF5", section);
            if (adaptor == nullptr) return false;
            adaptor->setH5Group(&sectionGroup);
            adaptor->setDataObject(section);
            adaptor->setWriter(_writer);
            bool w = adaptor->adaptW();
            isW &= w;
            delete adaptor;
        }
        return isW;
    }

    bool FITKRadiossDataCaseAdaptor::readInteraction(H5::Group& g)
    {
        H5::Group group = g.openGroup("Interaction");
        bool isR = true;
        Radioss::FITKInteractionManager* interactionMgr = _caseData->getInteractionManager();
        if (interactionMgr == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _reader->getHDF5FileTool();
        if (h5tools == nullptr) return false;
        // 获取所有子组
        hsize_t numObjs = group.getNumObjs();
        for (hsize_t i = 0; i < numObjs; i++)
        {
            // 获取子组名称
            std::string objName = group.getObjnameByIdx(i);
            H5::Group interactionGroup = group.openGroup(objName);
            std::string type{};
            h5tools->readGroupStr(interactionGroup, "InteractionType", type);
            Radioss::FITKAbstractInteraction* interaction = nullptr;
            if (type == "/INTER/TYPE2")
                interaction = new Radioss::FITKInteractionKinematicTied();
            
            if (!interaction) return false;
            interaction->setDataObjectName(QString::fromStdString(objName));
            // 创建并配置适配器
            Interface::FITKRadiossDataHDF5Adaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataHDF5Adaptor>("HDF5", interaction);
            if (adaptor == nullptr) {
                isR = false;
                delete interaction;
                continue;
            }
            adaptor->setH5Group(&interactionGroup);
            adaptor->setDataObject(interaction);
            adaptor->setReader(_reader);
            bool r = adaptor->adaptR();
            isR &= r;
            if (r)
            {
                interactionMgr->appendDataObj(interaction);
            }
            else
                delete interaction;
            delete adaptor;
        }
        return isR;
    }
    bool FITKRadiossDataCaseAdaptor::writeInteraction(H5::Group& g)
    {
        H5::Group group = g.createGroup("Interaction");
        bool isW = true;
        Radioss::FITKInteractionManager* interactionMgr = _caseData->getInteractionManager();
        if (interactionMgr == nullptr) return false;
        int count = interactionMgr->getDataCount();
        for (int i = 0; i < count; i++)
        {
            Radioss::FITKAbstractInteraction* interaction = interactionMgr->getDataByIndex(i);
            if (!interaction) return false;
            QString name = interaction->getDataObjectName();
            H5::Group interactionGroup = group.createGroup(name.toStdString());
            Interface::FITKRadiossDataHDF5Adaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataHDF5Adaptor>("HDF5", interaction);
            if (adaptor == nullptr) return false;
            adaptor->setH5Group(&interactionGroup);
            adaptor->setDataObject(interaction);
            adaptor->setWriter(_writer);
            bool w = adaptor->adaptW();
            isW &= w;
            delete adaptor;
        }
        return isW;
    }

    bool FITKRadiossDataCaseAdaptor::readCurve(H5::Group& g)
    {
        H5::Group group = g.openGroup("Curve");
        bool isR = true;
        Radioss::FITKNonLinerCurveManager*  curveMgr = _caseData->getNonLinerCurveManager();
        if (curveMgr == nullptr) return false;
        // 获取所有子组
        hsize_t numObjs = group.getNumObjs();
        for (hsize_t i = 0; i < numObjs; i++)
        {
            // 获取子组名称
            std::string objName = group.getObjnameByIdx(i);
            H5::Group curveGroup = group.openGroup(objName);
            Radioss::FITKNonLinerCurve* curve = new Radioss::FITKNonLinerCurve();
            curve->setDataObjectName(QString::fromStdString(objName));
            // 创建并配置适配器
            Interface::FITKRadiossDataNonLinerCurveAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataNonLinerCurveAdaptor>("HDF5", curve);
            if (adaptor == nullptr) {
                isR = false;
                delete curve;
                continue;
            }
            adaptor->setH5Group(&curveGroup);
            adaptor->setDataObject(curve);
            adaptor->setReader(_reader);
            bool r = adaptor->adaptR();
            isR &= r;
            if (r)
            {
                curveMgr->appendDataObj(curve);
            }
            else
                delete curve;
            delete adaptor;
        }
        return isR;
    }
    bool FITKRadiossDataCaseAdaptor::writeCurve(H5::Group& g)
    {
        H5::Group group = g.createGroup("Curve");
        bool isW = true;
        Radioss::FITKNonLinerCurveManager*  curveMgr = _caseData->getNonLinerCurveManager();
        if (curveMgr == nullptr) return false;
        int count = curveMgr->getDataCount();
        for (int i = 0; i < count; i++)
        {
            Radioss::FITKNonLinerCurve* curve = curveMgr->getDataByIndex(i);
            if (!curve) return false;
            QString name = curve->getDataObjectName();
            H5::Group curveGroup = group.createGroup(name.toStdString());
            Interface::FITKRadiossDataNonLinerCurveAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataNonLinerCurveAdaptor>("HDF5", curve);
            if (adaptor == nullptr) return false;
            adaptor->setH5Group(&curveGroup);
            adaptor->setDataObject(curve);
            adaptor->setWriter(_writer);
            bool w = adaptor->adaptW();
            isW &= w;
            delete adaptor;
        }
        return isW;
    }

    bool FITKRadiossDataCaseAdaptor::readInitialField(H5::Group& g)
    {
        H5::Group group = g.openGroup("InitialField");
        bool isR = true;
        Radioss::FITKInitialFieldManager* initialFieldMgr = _caseData->getInitialFieldManager();
        if (initialFieldMgr == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _reader->getHDF5FileTool();
        if (h5tools == nullptr) return false;
        // 获取所有子组
        hsize_t numObjs = group.getNumObjs();
        for (hsize_t i = 0; i < numObjs; i++)
        {
            // 获取子组名称
            std::string objName = group.getObjnameByIdx(i);
            H5::Group initialFieldGroup = group.openGroup(objName);
            std::string type{};
            h5tools->readGroupStr(initialFieldGroup, "InitialFieldKeyWord", type);
            Radioss::FITKAbstractInitialField* initialField = nullptr;
            if (type == "/INIVEL/TRA")
                initialField = new Radioss::FITKInitialFieldTra();

            if (!initialField) return false;
            initialField->setDataObjectName(QString::fromStdString(objName));
            // 创建并配置适配器
            Interface::FITKRadiossDataInitialFieldTraAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataInitialFieldTraAdaptor>("HDF5", initialField);
            if (adaptor == nullptr) {
                isR = false;
                delete initialField;
                continue;
            }
            adaptor->setH5Group(&initialFieldGroup);
            adaptor->setDataObject(initialField);
            adaptor->setReader(_reader);
            bool r = adaptor->adaptR();
            isR &= r;
            if (r)
            {
                initialFieldMgr->appendDataObj(initialField);
            }
            else
                delete initialField;
            delete adaptor;
        }
        return isR;
    }
    bool FITKRadiossDataCaseAdaptor::writeInitialField(H5::Group& g)
    {
        H5::Group group = g.createGroup("InitialField");
        bool isW = true;
        Radioss::FITKInitialFieldManager* initialFieldMgr = _caseData->getInitialFieldManager();
        if (initialFieldMgr == nullptr) return false;
        int count = initialFieldMgr->getDataCount();
        for (int i = 0; i < count; i++)
        {
            Radioss::FITKAbstractInitialField* initialField = initialFieldMgr->getDataByIndex(i);
            if (!initialField) return false;
            QString name = initialField->getDataObjectName();
            H5::Group initialFieldGroup = group.createGroup(name.toStdString());
            Interface::FITKRadiossDataInitialFieldTraAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataInitialFieldTraAdaptor>("HDF5", initialField);
            if (adaptor == nullptr) return false;
            adaptor->setH5Group(&initialFieldGroup);
            adaptor->setDataObject(initialField);
            adaptor->setWriter(_writer);
            bool w = adaptor->adaptW();
            isW &= w;
            delete adaptor;
        }
        return isW;
    }

    bool FITKRadiossDataCaseAdaptor::readGravity(H5::Group& g)
    {
        H5::Group group = g.openGroup("Gravity");
        bool isR = true;
        Radioss::FITKGravityManager* gravityMgr = _caseData->getGravityManager();
        if (gravityMgr == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _reader->getHDF5FileTool();
        if (h5tools == nullptr) return false;
        // 获取所有子组
        hsize_t numObjs = group.getNumObjs();
        for (hsize_t i = 0; i < numObjs; i++)
        {
            // 获取子组名称
            std::string objName = group.getObjnameByIdx(i);
            H5::Group gravityGroup = group.openGroup(objName);
            
            Radioss::FITKGravity* gravity = new Radioss::FITKGravity();
            if (!gravity) return false;

            gravity->setDataObjectName(QString::fromStdString(objName));
            // 创建并配置适配器
            Interface::FITKRadiossDataGravityAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataGravityAdaptor>("HDF5", gravity);
            if (adaptor == nullptr) {
                isR = false;
                delete gravity;
                continue;
            }
            adaptor->setH5Group(&gravityGroup);
            adaptor->setDataObject(gravity);
            adaptor->setReader(_reader);
            bool r = adaptor->adaptR();
            isR &= r;
            if (r)
            {
                gravityMgr->appendDataObj(gravity);
            }
            else
                delete gravity;
            delete adaptor;
        }
        return isR;
    }
    bool FITKRadiossDataCaseAdaptor::writeGravity(H5::Group& g)
    {
        H5::Group group = g.createGroup("Gravity");
        bool isW = true;
        Radioss::FITKGravityManager* gravityMgr = _caseData->getGravityManager();
        if (gravityMgr == nullptr) return false;
        int count = gravityMgr->getDataCount();
        for (int i = 0; i < count; i++)
        {
            Radioss::FITKGravity* gravity = gravityMgr->getDataByIndex(i);
            if (!gravity) return false;
            QString name = gravity->getDataObjectName();
            H5::Group gravityGroup = group.createGroup(name.toStdString());
            Interface::FITKRadiossDataGravityAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataGravityAdaptor>("HDF5", gravity);
            if (adaptor == nullptr) return false;
            adaptor->setH5Group(&gravityGroup);
            adaptor->setDataObject(gravity);
            adaptor->setWriter(_writer);
            bool w = adaptor->adaptW();
            isW &= w;
            delete adaptor;
        }
        return isW;
    }

    bool FITKRadiossDataCaseAdaptor::readRigidWall(H5::Group& g)
    {
        H5::Group group = g.openGroup("RigidWall");
        bool isR = true;
        Radioss::FITKRadiossRWallManager* wallMgr = _caseData->getRWallManager();
        if (wallMgr == nullptr) return false;
        IO::FITKHDF5FileTool* h5tools = _reader->getHDF5FileTool();
        if (h5tools == nullptr) return false;
        // 获取所有子组
        hsize_t numObjs = group.getNumObjs();
        for (hsize_t i = 0; i < numObjs; i++)
        {
            // 获取子组名称
            std::string objName = group.getObjnameByIdx(i);
            H5::Group wallGroup = group.openGroup(objName);
            std::string type{};
            h5tools->readGroupStr(wallGroup, "RWallKeyWord", type);
            Radioss::FITKAbstractRWall* wall = nullptr;
            if (type == "/RWALL/PLANE")
                wall = new Radioss::FITKRWallPlane();
            else if (type == "/RWALL/CYL")
                wall = new Radioss::FITKRWallCylinder();
            if (!wall) return false;
            wall->setDataObjectName(QString::fromStdString(objName));
            // 创建并配置适配器
            Interface::FITKRadiossDataHDF5Adaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataHDF5Adaptor>("HDF5", wall);
            if (adaptor == nullptr) {
                isR = false;
                delete wall;
                continue;
            }
            adaptor->setH5Group(&wallGroup);
            adaptor->setDataObject(wall);
            adaptor->setReader(_reader);
            bool r = adaptor->adaptR();
            isR &= r;
            if (r)
            {
                wallMgr->appendDataObj(wall);
            }
            else
                delete wall;
            delete adaptor;
        }
        return isR;
    }
    bool FITKRadiossDataCaseAdaptor::writeRigidWall(H5::Group& g)
    {
        H5::Group group = g.createGroup("RigidWall");
        bool isW = true;
        Radioss::FITKRadiossRWallManager*  wallMgr = _caseData->getRWallManager();
        if (wallMgr == nullptr) return false;
        int count = wallMgr->getDataCount();
        for (int i = 0; i < count; i++)
        {
            Radioss::FITKAbstractRWall* wall = wallMgr->getDataByIndex(i);
            if (!wall) return false;
            QString name = wall->getDataObjectName();
            H5::Group wallGroup = group.createGroup(name.toStdString());
            Interface::FITKRadiossDataHDF5Adaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataHDF5Adaptor>("HDF5", wall);
            if (adaptor == nullptr) return false;
            adaptor->setH5Group(&wallGroup);
            adaptor->setDataObject(wall);
            adaptor->setWriter(_writer);
            bool w = adaptor->adaptW();
            isW &= w;
            delete adaptor;
        }
        return isW;
    }
    bool FITKRadiossDataCaseAdaptor::readSolverSetting(H5::Group& g)
    {
        H5::Group group = g.openGroup("SolverSetting");
        bool isR = true;
        Radioss::FITKRadiossSolverSettings* solverSettings = _caseData->getSolverSettings();
        if (!solverSettings)return false;
        Interface::FITKRadiossDataSolverSettingsAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataSolverSettingsAdaptor>("HDF5", "Radioss::FITKRadiossSolverSettings");
        if (adaptor == nullptr) return false;
        adaptor->setH5Group(&group);
        adaptor->setDataObject(solverSettings);
        adaptor->setReader(_reader);
        bool r = adaptor->adaptR();
        isR &= r;
        delete adaptor;
        return isR;
    }
    bool FITKRadiossDataCaseAdaptor::writeSolverSetting(H5::Group& g)
    {
        H5::Group group = g.createGroup("SolverSetting");
        bool isW = true;
        Radioss::FITKRadiossSolverSettings* solverSettings = _caseData->getSolverSettings();
        if (!solverSettings)return false;
        Interface::FITKRadiossDataSolverSettingsAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataSolverSettingsAdaptor>("HDF5", "Radioss::FITKRadiossSolverSettings");
        if (adaptor == nullptr) return false;
        adaptor->setH5Group(&group);
        adaptor->setDataObject(solverSettings);
        adaptor->setWriter(_writer);
        bool w = adaptor->adaptW();
        isW &= w;
        delete adaptor;
        return isW;
    }

    bool FITKRadiossDataCaseAdaptor::readResultRequest(H5::Group& g)
    {
        H5::Group group = g.openGroup("ResultRequest");
        bool isR = true;
        Radioss::FITKRadiossResultRequest* resultRequest = _caseData->getResultRequest();
        if (!resultRequest)return false;
        Interface::FITKRadiossDataResultRequestAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataResultRequestAdaptor>("HDF5", "Radioss::FITKRadiossResultRequest");
        if (adaptor == nullptr) return false;
        adaptor->setH5Group(&group);
        adaptor->setDataObject(resultRequest);
        adaptor->setReader(_reader);
        bool r = adaptor->adaptR();
        isR &= r;
        delete adaptor;
        return isR;
    }
    bool FITKRadiossDataCaseAdaptor::writeResultRequest(H5::Group& g)
    {
        H5::Group group = g.createGroup("ResultRequest");
        bool isW = true;
        Radioss::FITKRadiossResultRequest* resultRequest = _caseData->getResultRequest();
        if (!resultRequest)return false;
        Interface::FITKRadiossDataResultRequestAdaptor* adaptor = FITKIOADAPTORFACTORY->createT<Interface::FITKRadiossDataResultRequestAdaptor>("HDF5", "Radioss::FITKRadiossResultRequest");
        if (adaptor == nullptr) return false;
        adaptor->setH5Group(&group);
        adaptor->setDataObject(resultRequest);
        adaptor->setWriter(_writer);
        bool w = adaptor->adaptW();
        isW &= w;
        delete adaptor;
        return isW;
    }
}
